package com.example.his.api.front.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.his.api.db.dao.CustomerDao;
import com.example.his.api.db.dao.OrderDao;
import com.example.his.api.db.pojo.CustomerEntity;
import com.example.his.api.front.controller.form.UpdateCustomerForm;
import com.example.his.api.front.service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Service("FrontCustomerServiceImpl")
@Slf4j
public class CustomerServiceImpl extends ServiceImpl<CustomerDao, CustomerEntity> implements CustomerService {

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private OrderDao orderDao;

    @Override
    public boolean sendSmsCode(String tel) {

        //生成六位随机数
        String code = RandomUtil.randomNumbers(6);
        log.info("验证码为：={}",code);

        //判断两分钟内是否重复点击发送验证码
        String resKey = "sms_code_refresh_" + tel;
        //如果有key 就说明两分钟内发送过
        if (Boolean.TRUE.equals( redisTemplate.hasKey(resKey))){
            return false;
        }

        //创建两分钟重发标志key
        redisTemplate.opsForValue().set(resKey,code,2, TimeUnit.MINUTES);

        //真正发送给客户的code  缓存5分钟
        String key = "sms_code_" + tel;
        redisTemplate.opsForValue().set(key,code,5, TimeUnit.MINUTES);

        //TODO 调用第三方服务
        return true;
    }

    @Override
    @Transactional
    public HashMap login(String tel, String code) {
        HashMap<String, Object> map = new HashMap<>();

        //判断验证码是否正确
        String key = "sms_code_" + tel;
        if (Boolean.FALSE.equals(redisTemplate.hasKey(key))){
            map.put("result",false);
            map.put("msg","验证码错误");
            return map;
        }

        //删除key
        redisTemplate.delete(key);
        String resKey = "sms_code_refresh_" + tel;
        redisTemplate.delete(resKey);

        //根据手机号查询是否已经注册过
        CustomerEntity one = this.getOne(new LambdaQueryWrapper<CustomerEntity>()
                .eq(CustomerEntity::getTel, tel));
        if (Objects.isNull(one)){
            CustomerEntity customerEntity = new CustomerEntity();
            customerEntity.setTel(tel);
            baseMapper.insert(customerEntity);
            map.put("id", customerEntity.getId());

        }else {
            map.put("id", one.getId());
        }

        map.put("result", true);
        map.put("msg", "登陆成功");
        return map;
    }




    @Override
    public HashMap searchSummary(int id) {
        HashMap map = baseMapper.searchById(id);
        map.putAll(orderDao.searchFrontStatistic(id));
        return map;
    }

    @Override
    public boolean myUpdate(UpdateCustomerForm form) {
        return this.updateById(BeanUtil.toBean(form, CustomerEntity.class));
    }
}
